<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Article;
use app\common\model\Category;
use app\common\model\Link;
use app\common\model\Diary;
use app\common\model\Recommend;
use think\Db;
use think\Exception;
use think\db\Expression;
use think\Controller;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $article = new Article;
        $where = array(
          'status'=>'normal',
        );
        $where[]=['exp',Db::raw("FIND_IN_SET('index',biaoshi)")];
        $list = $article->where($where)->limit(0,3)->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function read(){
        $id = input('id/d',0);
        $article = new Article;
        $info = $article->where(array('id'=>$id))->find();
        $this->assign('info',$info);
        return $this->view->fetch();
    }
    public function article(){

        $category = new Category();
        $cate = $category->where(array('pid'=>0))->select();

        $page = input('page/d',0);
        $pagesize = input('pagesize/d',10);
        $title = input('title');
        if(!empty($title)){
            $where['title'] = array('like',"%".$title."%");
        }
        $article = new Article;
        $where = array(
            'status'=>'normal',
        );
        $where[]=['exp',Db::raw("FIND_IN_SET('index',biaoshi)")];
        $list = $article->where($where)->limit($page,$pagesize)->order('sort','desc')->select();
        $this->assign('list',$list);
        $this->assign('cate',$cate);


        return $this->view->fetch();
    }
    public function link(){
        $Link = new Link;
        $list = $Link->where([])->order('sort','desc')->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function diary(){
        $Diary = new Diary;
        $list = $Diary->order('id','desc')->select();
        $list = collection($list)->toArray();
        $newlist = [];
        foreach($list as $v){
            $tt = date('Y',$v['createtime']);
            $newlist[$tt][] = $v;
        }
        $this->assign('list',$newlist);
        return $this->view->fetch();
    }
    public function message(){
        $list = Db::name('message')->where(array('pid'=>0))->order('id','desc')->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function addmsg(){

        $data = array(
          'content'=>input('editorContent'),
          'createtime'=>time()
        );
        $res = Db::name('message')->insert($data);
        if($res){
            $this->success('评价成功','index/message');
        }else{
            $this->error('index/message');
        }
    }
    public function share(){
        $list = Db::name('share')->where(array())->order('click','desc')->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function about(){
        $time = time()-strtotime('2016-6-8');
        $this->assign('time',ceil($time/86400));
        return $this->view->fetch();
    }
    public function recommend(){
        $list = Recommend::all();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
}
